package qeb.handheldmedical.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.R;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年10月13日 下午2:54:33
 * @declaration 分组病人
 */
public class PatientGroupAdapter extends BaseExpandableListAdapter {

	private static final String TAG = "PatientGroupAdapter";

	private int mCount;
	private Context mContext;
	private JSONArray patientJsonArray = null;

	public PatientGroupAdapter(Context context, JSONArray patientJsonArray) {
		this.mCount = patientJsonArray.length();
		this.mContext = context;
		this.patientJsonArray = patientJsonArray;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		JSONObject patient = new JSONObject();
		JSONObject work;
		try {
			work = patientJsonArray.getJSONObject(groupPosition);
			String patientJson = work.getString("PATIENTS");
			if (!patientJson.equals("")) {
				JSONArray patients = new JSONArray(patientJson);
				patient = patients.getJSONObject(childPosition);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return patient;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ItemHolder itemHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_inpatient, null);
			itemHolder = new ItemHolder();
			itemHolder.imgPatient = (ImageView) convertView
					.findViewById(R.id.imgPatient);
				itemHolder.txtch = (TextView) convertView.findViewById(R.id.txtch);
			itemHolder.txtbah = (TextView) convertView
					.findViewById(R.id.txtBah);
			itemHolder.txtPatientname = (TextView) convertView
					.findViewById(R.id.txtPatientname);
			itemHolder.txtPatientinfo = (TextView) convertView
					.findViewById(R.id.txtPatientinfo);
			itemHolder.txtHiddpatientinfo = (TextView) convertView
					.findViewById(R.id.txtHiddpatientinfo);
			itemHolder.txtPsjg = (TextView) convertView
					.findViewById(R.id.txtPsjg);
			convertView.setTag(itemHolder);
		} else {
			itemHolder = (ItemHolder) convertView.getTag();
		}

		JSONObject work;
		try {
			work = patientJsonArray.getJSONObject(groupPosition);
			String strPatient = work.getString("PATIENTS");
			if (!strPatient.equals("")) {
				JSONArray patients = new JSONArray(strPatient);
				JSONObject patient = patients.getJSONObject(childPosition);

				itemHolder.txtch.setText(" " + patient.getString("CH"));
				itemHolder.txtbah.setText(patient.getString("BAH"));
				itemHolder.txtPatientname.setText(patient.getString("XM"));
				itemHolder.txtPatientinfo
						.setText(" " + patient.getString("ZD"));
				if (patient.getString("SEXID").equals("0"))
					itemHolder.imgPatient
							.setBackgroundResource(R.drawable.nan01);
				else
					itemHolder.imgPatient
							.setBackgroundResource(R.drawable.nv01);
				String strPsjg = patient.getString("PSJG");
				if (!strPsjg.equals("")) {
					itemHolder.txtPsjg
							.setTextColor(Color.parseColor("#FFA500"));
					itemHolder.txtPsjg.setText(patient.getString("PSJG"));
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		int patietnlistcount = 0;
		JSONObject work;
		try {
			work = patientJsonArray.getJSONObject(groupPosition);
			String patientJson = work.getString("PATIENTS");
			if (!patientJson.equals("")) {
				JSONArray patients = new JSONArray(patientJson);
				patietnlistcount = patients.length();
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return patietnlistcount;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		JSONObject work = new JSONObject();
		try {
			work = patientJsonArray.getJSONObject(groupPosition);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return work;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		GroupHolder groupHolder = null;

		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.patient_group, null);

			groupHolder = new GroupHolder();
			groupHolder.tvWordName = (TextView) convertView
					.findViewById(R.id.tvWordName);
			groupHolder.tvPatientCount = (TextView) convertView
					.findViewById(R.id.tvPatientCount);

			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		JSONObject work;
		try {
			work = patientJsonArray.getJSONObject(groupPosition);
			groupHolder.tvWordName.setText(work.getString("TITLE"));
			groupHolder.tvPatientCount.setText(work.getString("ROWCOUNT"));

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	class GroupHolder {
		public TextView tvWordName;
		public TextView tvPatientCount;
	}

	class ItemHolder {
		ImageView imgPatient;
		TextView txtch;
		TextView txtbah;
		TextView txtPatientname;
		TextView txtPatientinfo;
		TextView txtHiddpatientinfo;
		TextView txtPsjg;
	}
}
